package com.baosight.sczx.zm.cp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanCM;

/**
 * <p>Title: ServiceZMCP02.java</p>
 * <p>Description: 采煤专业计划明细</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-24 下午02:51:04  张小飞  Create
 *</pre>
 */
public class ServiceZMCP02 extends ServiceEPBase {
	String fdProfessionalplanId;
	String fdProduceplanId;
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午02:51:38 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);

		outInfo.set("fdCountBeginDate", SystemSettingCP.getInstance().getBeginDate());
		return outInfo;
	}	
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午02:51:58 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getString("fdProfessionalplanId")!=null)fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		StaticTools.setQueryParameters(inInfo, "fdProfessionalplanId", fdProfessionalplanId, true);//添加查询条件
		
		if(inInfo.getString("fdProduceplanId")!=null)fdProduceplanId = inInfo.getString("fdProduceplanId");
		StaticTools.setQueryParameters(inInfo, "fdProduceplanId", fdProduceplanId, true);//添加查询条件
		
		EiInfo outInfo=super.query(inInfo, "ZMCPProducePlanCM.query",  new ZMCPProducePlanCM(), false, null, EiConstant.queryBlock, "detail", "detail");

		outInfo.addBlock(Engineering.getEngineeringForCM("cmes.cp.engineering", "0", "", "1", false));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.coalType", true));//采煤方法
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.recoveryProcess", true));//采煤方式
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.conditon", true));//生产条件
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.coalKind", true));//煤种
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.qualityGrade", true));//质量等级
		return outInfo;
	}
		
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午03:07:57 张小飞
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		//校验是否重复
		Map<String, String>	params = new HashMap<String, String>();
		params.put("fdProfessionalplanId", inInfo.getBlock("result").getCellStr(0, "fdProfessionalplanId"));
		params.put("fdProduceplanId", inInfo.getBlock("result").getCellStr(0, "fdProduceplanId"));
		boolean validate = this.validate(inInfo, "ZMCPProducePlanCM.validate", params);
		if(validate) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));
			inInfo.getBlock("result").setCell(0, "fdCreateDepartment", UserSession.getInSessionProperty("orgName"));
			inInfo.getBlock("result").setCell(0, "fdCreatorId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(0, "fdCreator", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDate());
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_CM"));
			
			EiInfo outInfo = super.insert(inInfo, "ZMCPProducePlanCM.insert");
	
			super.update(inInfo,"ZMCPProducePlanCM.updateProfessionalplanSum");
			super.update(inInfo,"ZMCPProducePlanCM.updateProduceplanSum");
			
			return outInfo;
		}
	}		
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午04:20:50 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		params.put("fdProfessionalplanId", inInfo.getBlock("result").getCellStr(0, "fdProfessionalplanId"));
		params.put("fdProduceplanId", inInfo.getBlock("result").getCellStr(0, "fdProduceplanId"));
		boolean validate = this.validate(inInfo, "ZMCPProducePlanCM.validate", params);
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCPProducePlanCM.update");
			
			super.update(inInfo,"ZMCPProducePlanCM.updateProfessionalplanSum");//更新主表计划总量
			super.update(inInfo,"ZMCPProducePlanCM.updateProduceplanSum");//更新生产计划总量
			
			return outInfo;
		}
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午04:30:17 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCPProducePlanCM.delete","detail");
//		super.update(inInfo,"ZMCPProducePlanCM.updateProfessionalplanSum");//更新主表计划总量
		if(fdProfessionalplanId!=null)
		{
			HashMap hs=new HashMap();
			hs.put("fdProfessionalplanId", fdProfessionalplanId);
			dao.update("ZMCPProducePlanCM.updateProfessionalplanSum", hs);
		}
//		super.update(inInfo,"ZMCPProducePlanCM.updateProduceplanSum");//更新生产计划总量
		if(fdProduceplanId!=null)
		{
			HashMap hs=new HashMap();
			hs.put("fdProduceplanId", fdProduceplanId);
			dao.update("ZMCPProducePlanCM.updateProduceplanSum", hs);
		}
		
		EiInfo outInfo=query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}	
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 新增，修改前的检验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-24下午03:39:50 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdPlanYear = inInfo.getBlock("result").getCellStr(0, "fdPlanYear");
		String fdPlanMonth = inInfo.getBlock("result").getCellStr(0, "fdPlanMonth");
		String fdProjectId = inInfo.getBlock("result").getCellStr(0, "fdProjectId");
		String fdDepartmentId = inInfo.getBlock("result").getCellStr(0, "fdDepartmentId");
		
		params.put("fdPlanYear", fdPlanYear);
		params.put("fdPlanMonth", fdPlanMonth);
		params.put("fdProjectId", fdProjectId);
		params.put("fdDepartmentId", fdDepartmentId);
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "采煤计划每月同一施工部门针对同一工程仅可新增一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}

}
